package com.liushuang.waiwaiim.controller.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.liushuang.waiwaiim.R;
import com.liushuang.waiwaiim.controller.activity.InviteActivity;
import com.liushuang.waiwaiim.model.bean.InvationInfo;
import com.liushuang.waiwaiim.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

public class InviteAdapter extends BaseAdapter {

    private Context mContext;
    private List<InvationInfo> mInvationInfos = new ArrayList<>();
    private InvationInfo mInvationInfo;
    private OnInviteListener mOnInviteListener;

    public InviteAdapter(Context context, OnInviteListener onInviteListener) {
        mContext = context;
        mOnInviteListener = onInviteListener;
    }

    //刷新数据的方法
    public void refresh(List<InvationInfo> invationInfos){

        if (invationInfos != null && invationInfos.size() >= 0){

            //现将列表中原有数据清空,不然通过addAll方法后可能会有重复的数据
            mInvationInfos.clear();
            mInvationInfos.addAll(invationInfos);
            //Adapter中的数据在更新后，要及时进行刷新
            notifyDataSetChanged();
        }
    }
    @Override
    public int getCount() {
        return mInvationInfos == null ? 0 : mInvationInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInvationInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        mInvationInfo = mInvationInfos.get(position);

        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_invite, parent, false);

            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        UserInfo user = mInvationInfo.getUser();

        if (user != null){//联系人
            //名称的展示
            viewHolder.mTvName.setText(mInvationInfo.getUser().getName());
            viewHolder.mTvAccept.setVisibility(View.GONE);
            viewHolder.mTvReject.setVisibility(View.GONE);

            if (mInvationInfo.getStatus() == InvationInfo.InvitationStatus.NEW_INVITE){
                if (mInvationInfo.getReason() == null){
                    viewHolder.mTvReason.setText("添加好友");
                }else {
                    viewHolder.mTvReason.setText(mInvationInfo.getReason());
                }

                viewHolder.mTvAccept.setVisibility(View.VISIBLE);
                viewHolder.mTvReject.setVisibility(View.VISIBLE);
            } else if (mInvationInfo.getStatus() == InvationInfo.InvitationStatus.INVITE_ACCEPT){

                if (mInvationInfo.getReason() == null){
                    viewHolder.mTvReason.setText("接受邀请");
                }else {
                    viewHolder.mTvReason.setText(mInvationInfo.getReason());
                }

            } else if (mInvationInfo.getStatus() == InvationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER){

                if (mInvationInfo.getReason() == null){
                    viewHolder.mTvReason.setText("邀请被接受");
                }else {
                    viewHolder.mTvReason.setText(mInvationInfo.getReason());
                }

            }

            //接受按钮的处理事件
            viewHolder.mTvAccept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.onAccept(mInvationInfo);
                }
            });

            //拒绝按钮的处理事件
            viewHolder.mTvReject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.onReject(mInvationInfo);
                }
            });
        }else {

        }
        return convertView;
    }

     private class ViewHolder {

        private TextView mTvName;
        private TextView mTvReason;
        private TextView mTvAccept;
        private TextView mTvReject;

        public ViewHolder(View view) {
            mTvName = view.findViewById(R.id.tv_invite_name);
            mTvReason = view.findViewById(R.id.tv_invite_reason);
            mTvAccept = view.findViewById(R.id.tv_invite_accept);
            mTvReject = view.findViewById(R.id.tv_invite_reject);
        }
    }

    public interface OnInviteListener {
        // 联系人接受按钮的点击事件
        void onAccept(InvationInfo invationInfo);

        // 联系人拒绝按钮的点击事件
        void onReject(InvationInfo invationInfo);

        // 接受邀请按钮处理
        void onInviteAccept(InvationInfo invationInfo);
        // 拒绝邀请按钮处理
        void onInviteReject(InvationInfo invationInfo);

        // 接受申请按钮处理
        void onApplicationAccept(InvationInfo invationInfo);
        // 拒绝申请按钮处理
        void onApplicationReject(InvationInfo invationInfo);
    }
}
